﻿using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using RAD.Modules.AD.Interfaces;
using RAD.Modules.AD.PresentationModels;
using RAD.Modules.AD.Services;
using RAD.Modules.AD.Views;

namespace RAD.Modules.AD
{
    /// <summary>
    /// Модуль ADModule реализует функциональность для работы с Active Directory (далее AD)
    /// </summary>
    public class ADModule : IModule
    {
        private IUnityContainer _container;
        private IRegionManager _regionManager;

        public ADModule(IUnityContainer container, IRegionManager regionManager)
        {
            _container = container;
            _regionManager = regionManager;
        }

        #region IModule Members

        public void Initialize()
        {
            RegisterViewsAndServices();

            // Добавляем регион AD
            IADPresenter presenter = _container.Resolve<IADPresenter>();
            _regionManager.Regions["ADRegion"].Add(presenter.View);

            // Добавляем регион описания
            IDetailsViewPresenter detailsPresenter = _container.Resolve<IDetailsViewPresenter>();
            _regionManager.Regions["DetailsRegion"].Add(detailsPresenter.View);
        }

        protected void RegisterViewsAndServices()
        {
            _container.RegisterType<IADPresentationModel, ADPresentationModel>(new ContainerControlledLifetimeManager());
            _container.RegisterType<INetService, NetService>(new ContainerControlledLifetimeManager());
            _container.RegisterType<IADView, ADView>();
            _container.RegisterType<IADPresenter, ADPresenter>();
            _container.RegisterType<IDetailsView, DetailsView>();
            _container.RegisterType<IDetailsViewPresenter, DetailsViewPresenter>();
        }

        #endregion
    }
}